/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.PayBillBean;
import com.minierp.bean.PayBillItemBean;
import com.minierp.dao.PayBillDao;
import com.minierp.model.PayBill;
import com.minierp.model.PayBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class PayBillValidator implements Validator {
	private PayBillDao payBillDao;

	public boolean supports(Class clazz) {
		return PayBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		PayBillBean bean = (PayBillBean) obj;
		PayBill form = bean.getPayBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("payBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				PayBill hasItems = this.payBillDao.get(form.getBillCode());
				if (hasItems != null && hasItems.getId() != form.getId()) {
					errors.rejectValue("payBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("payBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setPayDate(sf.parse(bean.getPayDateStr()));
			} catch (Exception arg13) {
				hasError = true;
				errors.rejectValue("payDateStr", "",
						"『付款日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getReceiverId() == 0L) {
				errors.rejectValue("payBill.receiverId", "", "请选择『收款单位』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("payBill.managerId", "", "请选择『经办人员』");
				hasError = true;
			} else if (form.getDepartmentId() == 0L) {
				errors.rejectValue("payBill.departmentId", "", "请选择『部门』");
				hasError = true;
			} else if (form.getBankAccountId() == 0L) {
				errors.rejectValue("payBill.bankAccountId", "", "请选择『付款账户』");
				hasError = true;
			} else {
				try {
					form.setCost(new BigDecimal(bean.getCostStr()));
				} catch (Exception arg12) {
					hasError = true;
					errors.rejectValue("costStr", "", "『付款金额』只能输入数字");
				}
			}
		}

		if (!hasError && form.getDescription() != null
				&& form.getDescription().length() >= 2000) {
			hasError = true;
			errors.rejectValue("payBill.description", "", "『摘要』不能超过2000个字符");
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 250) {
			hasError = true;
			errors.rejectValue("payBill.mark", "", "『备注』不能超过250个字符");
		}

		boolean arg15 = false;
		if (!hasError) {
			BigDecimal total = new BigDecimal(0);

			for (int i = 0; i < bean.getItems().size(); ++i) {
				PayBillItemBean cb = (PayBillItemBean) bean.getItems().get(i);
				if (!cb.getStatus().equals("DELETE")) {
					PayBillItem c = cb.getPayBillItem();

					try {
						c.setThisCost(new BigDecimal(cb.getThisCostStr()));
						total = total.add(c.getThisCost());
						if (c.getThisCost().compareTo(new BigDecimal(0)) != 0) {
							arg15 = true;
						}
					} catch (Exception arg14) {
						hasError = true;
						errors.rejectValue("items", "", "『本次结算金额』只能输入数字");
						break;
					}
				}
			}

			if (!hasError && !arg15) {
				hasError = true;
				errors.rejectValue("items", "", "付款明细不能为空或全部金额为0");
			}

			if (!hasError && total.compareTo(form.getCost()) != 0) {
				hasError = true;
				errors.rejectValue("items", "", "『付款金额』与『结算金额总数』不符");
			}
		}

	}

	public PayBillDao getPayBillDao() {
		return this.payBillDao;
	}

	public void setPayBillDao(PayBillDao payBillDao) {
		this.payBillDao = payBillDao;
	}
}